/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package queenb.web.controller;

import java.util.ArrayList;
import java.util.List;
import queenb.net.entitybean.S350;
import queenb.web.permission.store.S350Store;
import queenb.web.util.SessionProvider;

/**
 *
 * @author ems
 */
public class S350Controller  extends QueenbController{

    private String txtLeft="";
    private String txtRight="";
    private int PS200;
    private int actionReturnSave;

    public S350Controller(){
        Object ps200 = getRequest().getParameter("ps200");

        if(ps200 != null){
            PS200 = Integer.parseInt(ps200.toString());
            SessionProvider.setSessionMapValue("PS200",PS200);
        }
    }

    /**
     * @return the txtLeft
     */
    public String getTxtLeft() {
        List<S350> lstS250 = new ArrayList<S350>();
        txtLeft="";


            try {
              S350Store store = new S350Store();
              //lstS250 = store.findS250By( "0", 0,getLogin().getModule(), getLoginSession().getCompanyWorking());
              lstS250 = store.listS350L(getLogin().getModule(), getLogin().getCompanyNo(), PS200, 0,getLogin().getCharacter(), getLogin().getUserName());
            

            } catch (Exception ex) {

                System.out.println("----size" +ex.getMessage());
            }


        if (lstS250.size() > 0) {
            for (int i = 0; i < lstS250.size(); i++) {
                S350 s250 = lstS250.get(i);
                txtLeft += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                        + s250.getPs250() + "'>" + s250.getSv251() + "</div>";
            }
        }
        return txtLeft;
    }

    /**
     * @param txtLeft the txtLeft to set
     */
    public void setTxtLeft(String txtLeft) {
        this.txtLeft = txtLeft;
    }

    /**
     * @return the txtRight
     */
    public String getTxtRight() {

        List<S350> lstS350 = new ArrayList<S350>();
        String txtRightString = "";


        if(PS200 > 0){
                try {
                    S350Store store = new S350Store();
                    lstS350 = store.listS350R(getLogin().getModule(),getLogin().getCompanyNo(),PS200,0, getLogin().getUserName());

                } catch (Exception ex) {

                }
                if (lstS350 != null) {
                    if (lstS350.size() > 0) {
                        for (int i = 0; i < lstS350.size(); i++) {
                            S350 s350 = lstS350.get(i);
                            txtRightString += "<div class='destinationItem' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)'>";
                            txtRightString += "<div class='desNameItem' lang='" + s350.getFs250() + "'>" + s350.getSv251() + "</div>";
                            txtRightString += "</div>";
                        }
                    }
                }

        }
        txtRight = txtRightString;
        return txtRight;
    }

    /**
     * @param txtRight the txtRight to set
     */
    public void setTxtRight(String txtRight) {
        this.txtRight = txtRight;
    }

    /**
     * @return the PS200
     */
    public int getPS200() {
        return PS200;
    }

    /**
     * @param PS200 the PS200 to set
     */
    public void setPS200(int PS200) {
        this.PS200 = PS200;
    }

    /**
     * @return the actionReturnSave
     */
    public int getActionReturnSave() {
        ArrayList<Integer> listWork = new ArrayList<Integer>();

       // ArrayList<String> listtitle = new ArrayList<String>();
        ArrayList<Integer> back = new ArrayList<Integer>();
        ArrayList<Integer> ids350 = new ArrayList<Integer>();
        Object listIdWorkString = getRequest().getParameter("ids250");

        if (listIdWorkString != null ) {
            String[] listIdWork = listIdWorkString.toString().split("_");



            if (listIdWork.length > 0  )
                for (int i = 0; i < listIdWork.length; i++) {
                    if (!listIdWork[i].isEmpty()) {

                        listWork.add(Integer.parseInt(listIdWork[i]));


                    }

                }


        S350Store store = new S350Store();
        List<S350> list = new ArrayList<S350>();

                try {

                    list = store.listS350R(getLogin().getModule(),getLogin().getCompanyNo(),PS200,0, getLogin().getUserName());

                } catch (Exception ex) {
                    System.out.println("Error  " + ex.getMessage());
                }
                for( int i =  0 ; i < list.size(); i++){
                    back.add(list.get(i).getFs250());

                    ids350.add(list.get(i).getPs350());
                }


                 for(int i = 0; i< listWork.size() ; i ++){
                     if(listWork.get(i) == 0){
                          System.out.println("updateeeeeeeeeee00000000" );
                         continue;
                     }
//
                     if(!back.contains(listWork.get(i)) || back.isEmpty()){

                        try{
                            System.out.println("khong chua"  );
                            System.out.println("size"+listWork.size()  +"size"  );
                            int result = store.updateS350(0, getLogin().getModule() ,getLogin().getCompanyNo(),
                                    PS200,
                                    listWork.get(i), null, null,
                                   null, 0, null, null, null, getLogin().getUserName(),0);

                             System.out.println("update " +listWork.get(i)   );

                        } catch (Exception ex) {
                            System.out.println("Error  " + ex.getMessage());
                        }

                     }
                 }
                 for(int i = 0; i< back.size() ; i ++){
                     if(!listWork.contains(back.get(i))){
                         try {

                             store.deleteS350(PS200, back.get(i), null, getLogin().getUserName());

                             System.out.println("delete " +back.get(i) +"with" + PS200  );
                         }
                         catch (Exception ex){
                              System.out.println("Error  " + ex.getMessage());
                         }
                     }
                 }


            }
        return actionReturnSave;        
    }

    /**
     * @param actionReturnSave the actionReturnSave to set
     */
    public void setActionReturnSave(int actionReturnSave) {
        this.actionReturnSave = actionReturnSave;
    }
}
